package net.atomarrow.action;

import net.atomarrow.aop.ControllerInterceptor;
import net.atomarrow.controllers.Controller;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
/**
 * Action配置
 * @author Michael
 *
 */
public class Action {
    private Class<? extends Controller> controllerClass;

    private String methodName;//
    private Method method;
    private ParamInfo[] paramInfos;
    private String mappingUrl;
    private List<ControllerInterceptor> interceptors;

    public Class<? extends Controller> getControllerClass() {
        return controllerClass;
    }

    public void setControllerClass(Class<? extends Controller> controllerClass) {
        this.controllerClass = controllerClass;
    }

    public List<ControllerInterceptor> getInterceptors() {
        return interceptors;
    }

    public void setInterceptors(List<ControllerInterceptor> interceptors) {
        this.interceptors = interceptors;
    }
    /**
     * this is mapping methodName, if need real method name should use  getMethod.getName()<br>
     * eg.<br>
     * url("/aa/bb")<br>
     * public Render hello(){}<br>
     * 
     * the result is:<br>
     * methodName is bb<br>
     * method.getName() is hello
     * 
     */
    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
        this.paramInfos = analyseParamInfos(method.getParameters());
    }

    /**
     * 分析Render 方法的参数
     * @param parameters
     * @return
     */
    private ParamInfo[] analyseParamInfos(Parameter[] parameters) {
        if (parameters.length > 0) {
            ParamInfo[] infos = new ParamInfo[parameters.length];
            for (int i = 0; i < parameters.length; i++) {
                infos[i] = new ParamInfo(parameters[i]);
            }
            return infos;
        }
        return null;
    }

    public String getMappingUrl() {
        return mappingUrl;
    }

    public void setMappingUrl(String mappingUrl) {
        this.mappingUrl = mappingUrl;
    }

    public ParamInfo[] getParamInfos() {
        return paramInfos;
    }

    public void setParamInfos(ParamInfo[] paramInfos) {
        this.paramInfos = paramInfos;
    }


}
